package com.fangwaii.core.session.defaults;


import com.fangwaii.core.bind.IGenericReference;

import com.fangwaii.core.executor.Executor;
import com.fangwaii.core.executor.result.ExecutorResult;
import com.fangwaii.core.mapping.HttpStatement;
import com.fangwaii.core.session.Configuration;
import com.fangwaii.core.session.GatewaySession;

import java.util.Map;


/**
 * @author 小傅哥，微信：fustack
 * @description 默认网关会话实现类
 * @github github.com/fuzhengwei
 * @copyright 公众号：bugstack虫洞栈 | 博客：bugstack.cn - 沉淀、分享、成长，让自己和他人都能有所收获！
 */
public class DefaultGatewaySession implements GatewaySession {

    private Configuration configuration;
    private String uri;
    private Executor executor;

    public DefaultGatewaySession(Configuration configuration, String uri, Executor executor) {
        this.configuration = configuration;
        this.uri = uri;
        this.executor = executor;
    }

    @Override
    public Object get(String methodName, Map<String, Object> parameter) {
        /*
         * 调用服务
         * 封装参数 PS：为什么这样构建参数，可以参考测试案例；cn.bugstack.gateway.test.RPCTest
         * 01(允许)：java.lang.String
         * 02(允许)：cn.bugstack.gateway.rpc.dto.XReq
         * 03(拒绝)：java.lang.String, cn.bugstack.gateway.rpc.dto.XReq —— 不提供多参数方法的处理
         * */

        HttpStatement httpStatement = configuration.getHttpStatement(uri);

        ExecutorResult execute = executor.execute(httpStatement, parameter);

        return execute;



    }

    @Override
    public Object post(String methodName, Map<String, Object> parameter) {
        //目前只支持get请求
        return get(methodName, parameter);
    }

    @Override
    public IGenericReference getMapper() {
        return configuration.getMapper(uri, this);
    }

    @Override
    public Configuration getConfiguration() {
        return configuration;
    }

}
